home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_051 / bison / output.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  22KB  |  1,213 lines

  1. /* Output the generated parsing program for bison,
  2.    copyright (C) 1984 Bob Corbett and Richard Stallman
  3.  
  4.    Permission is granted to anyone to make or distribute verbatim copies of this program
  5.    provided that the copyright notice and this permission notice are preserved;
  6.    and provided that the recipient is not asked to waive or limit his right to
  7.    redistribute copies as permitted by this permission notice;
  8.    and provided that anyone possessing an executable copy
  9.    is granted access to copy the source code, in machine-readable form,
  10.    in some reasonable manner.
  11.  
  12.    Permission is granted to distribute derived works or enhanced versions of
  13.    this program under the above conditions with the additional condition
  14.    that the entire derivative or enhanced work
  15.    must be covered by a permission notice identical to this one.
  16.  
  17.    Anything distributed as part of a package containing portions derived
  18.    from this program, which cannot in current practice perform its function usefully
  19.    in the absense of what was derived directly from this program,
  20.    is to be considered as forming, together with the latter,
  21.    a single work derived from this program,
  22.    which must be entirely covered by a permission notice identical to this one
  23.    in order for distribution of the package to be permitted.
  24.  
  25.  In other words, you are welcome to use, share and improve this program.
  26.  You are forbidden to forbid anyone else to use, share and improve
  27.  what you give them.   Help stamp out software-hoarding!  */
  28.  
  29. /* functions to output parsing data to various files.  Entries are:
  30.  
  31.   output_headers ()
  32.  
  33. Output constant strings to the beginning of certain files.
  34.  
  35.   output_trailers()
  36.  
  37. Output constant strings to the ends of certain files.
  38.  
  39.   output ()
  40.  
  41. Output the parsing tables and the parser code to ftable.
  42.  
  43. The parser tables consist of:  (starred ones needed only for the semantic parser)
  44.  
  45. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  46.  
  47. * yyrhs = vector of items of all rules.
  48.         This is exactly what ritems contains.
  49.  
  50. * yyprhs[r] = index in yyrhs of first item for rule r.
  51.  
  52. yyr1[r] = symbol number of symbol that rule r derives.
  53.  
  54. yyr2[r] = number of symbols composing right hand side of rule r.
  55.  
  56. * yystos[s] = the symbol number of the symbol that leads to state s.
  57.  
  58. yydefact[s] = default rule to reduce with in state s,
  59.           when yytable doesn't specify something else to do.
  60.           Zero means the default is an error.
  61.  
  62. yydefgoto[i] = default state to go to after a reduction of a rule that
  63.            generates variable ntokens + i, except when yytable
  64.            specifies something else to do.
  65.  
  66. yypact[s] = index in yytable of the portion describing state s.
  67.             The lookahed token's type is used to index that portion
  68.             to find out what to do.
  69.  
  70.         If the value in yytable is positive,
  71.         we shift the token and go to that state.
  72.  
  73.         If the value is negative, it is minus a rule number to reduce by.
  74.  
  75.         If the value is zero, the default action from yydefact[s] is used.
  76.  
  77. yypgoto[i] = the index in yytable of the portion describing 
  78.              what to do after reducing a rule that derives variable i + ntokens.
  79.              This portion is indexed by the parser state number
  80.          as of before the text for this nonterminal was read.
  81.          The value from yytable is the state to go to.
  82.  
  83. yytable = a vector filled with portions for different uses,
  84.           found via yypact and yypgoto.
  85.  
  86. yycheck = a vector indexed in parallel with yytable.
  87.       It indicates, in a roundabout way, the bounds of the
  88.       portion you are trying to examine.
  89.  
  90.       Suppose that the portion of yytable starts at index p
  91.       and the index to be examined within the portion is i.
  92.       Then if yycheck[p+i] != i, i is outside the bounds
  93.       of what is actually allocated, and the default
  94.       (from yydefact or yydefgoto) should be used.
  95.       Otherwise, yytable[p+i] should be used.
  96.  
  97. YYFINAL = the state number of the termination state.
  98. YYFLAG = most negative short int.  Used to flag ??
  99. YYNTBASE = ntokens.
  100.  
  101. */
  102.  
  103. #include <stdio.h>
  104. #include "machine.h"
  105. #include "new.h"
  106. #include "files.h"
  107. #include "gram.h"
  108. #include "state.h"
  109.  
  110. #define    MAXTABLE 32767
  111.  
  112.  
  113. extern int tokensetsize;
  114. extern int final_state;
  115. extern core **state_table;
  116. extern shifts **shift_table;
  117. extern reductions **reduction_table;
  118. extern short *accessing_symbol;
  119. extern unsigned *LA;
  120. extern short *LAruleno;
  121. extern short *lookaheads;
  122. extern char *consistent;
  123. extern short *goto_map;
  124. extern short *from_state;
  125. extern short *to_state;
  126.  
  127.  
  128. static int nvectors;
  129. static int nentries;
  130. static short **froms;
  131. static short **tos;
  132. static short *tally;
  133. static short *width;
  134. static short *actrow;
  135. static short *state_count;
  136. static short *order;
  137. static short *base;
  138. static short *pos;
  139. static short *table;
  140. static short *check;
  141. static int lowzero;
  142. static int high;
  143.  
  144.  
  145.  
  146. static char *guardstr[] = {
  147.     "extern int yyerror;\n",
  148.     "extern int yycost;\n",
  149.     "extern char * yymsg;\n",
  150.     "extern YYSTYPE yyval;\n\n",
  151.     "yyguard(n, yyvsp, yylsp)\n",
  152.     "register int n;\n",
  153.     "register YYSTYPE *yyvsp;\n",
  154.     "register YYLTYPE *yylsp;\n",
  155.     "{\n",
  156.     "yyerror = 0;\n",
  157.     "yycost = 0;\n",
  158.     "yymsg = 0;\n",
  159.     "switch (n)\n",
  160.     "{",
  161.     NULL
  162. };
  163.  
  164. static char *actstr[] = {
  165.     "extern YYSTYPE yyval;\n",
  166.     "extern int yychar;\n",
  167.     "yyaction(n, yyvsp, yylsp)\n",
  168.     "register int n;\n",
  169.     "register YYSTYPE *yyvsp;\n",
  170.     "register YYLTYPE *yylsp;\n",
  171.     "{\n",
  172.     "switch (n)\n",
  173.     "{",
  174.     NULL
  175. };
  176.  
  177. #define    ACTSTR_SIMPLE    "\n  switch (yyn) {\n"
  178.  
  179. output_headers()
  180. {
  181.   if (semantic_parser) {
  182.     fprintf(fguard, "\n#include \"%s\"\n", attrsfile);
  183.     emit(guardstr);
  184.     fprintf(faction, "\n#include \"%s\"\n", attrsfile);
  185.     emit(actstr);
  186.   } else {
  187.     fprintf(faction, ACTSTR_SIMPLE);
  188.   }
  189.   if (semantic_parser)
  190.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  191.   fprintf(ftable, "#include <stdio.h>\n\n");
  192. }
  193.  
  194.  
  195.  
  196. output_trailers()
  197. {
  198.   if (semantic_parser)
  199.     {
  200.       fprintf(fguard, "\n    }\n}\n");
  201.       fprintf(faction, "\n    }\n}\n");
  202.     }
  203.   else
  204.     fprintf(faction, "\n}\n");
  205. }
  206.  
  207.  
  208.  
  209. output()
  210. {
  211.   free_itemsets();
  212.   output_defines();
  213.   output_token_translations();
  214.   if (semantic_parser)
  215.     output_gram();
  216.   FREE(ritem);
  217.   if (semantic_parser)
  218.     output_stos();
  219.   output_rule_data();
  220.   output_actions();
  221.   output_parser();
  222.   output_program();
  223. }
  224.  
  225.  
  226.  
  227. output_token_translations()
  228. {
  229.   register int i, j;
  230.  
  231.   if (translations)
  232.     {
  233.       fprintf(ftable, "\n#define YYTRANSLATE(x) (yytranslate[x])\n");
  234.     
  235.       if (ntokens < 127)  /* play it very safe; check maximum element value.  */
  236.         fprintf(ftable, "\nstatic char yytranslate[] = {     0");
  237.       else
  238.     fprintf(ftable, "\nstatic short yytranslate[] = {     0");
  239.     
  240.       j = 10;
  241.       for (i = 1; i <= max_user_token_number; i++)
  242.     {
  243.       putc(',', ftable);
  244.     
  245.       if (j >= 10)
  246.         {
  247.           putc('\n', ftable);
  248.           j = 1;
  249.         }
  250.       else
  251.         {
  252.           j++;
  253.         }
  254.     
  255.       fprintf(ftable, "%6d", token_translations[i]);
  256.     }
  257.     
  258.       fprintf(ftable, "\n};\n");
  259.     }
  260.   else
  261.     {
  262.       fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  263.     } 
  264. }
  265.  
  266.  
  267.  
  268. output_gram()
  269. {
  270.   register int i;
  271.   register int j;
  272.   register short *sp;
  273.  
  274.   fprintf(ftable, "\nstatic short yyprhs[] = {     0");
  275.  
  276.   j = 10;
  277.   for (i = 1; i < nrules; i++)
  278.     {
  279.       putc(',', ftable);
  280.  
  281.       if (j >= 10)
  282.     {
  283.       putc('\n', ftable);
  284.       j = 1;
  285.     }
  286.       else
  287.     {
  288.       j++;
  289.     }
  290.  
  291.       fprintf(ftable, "%6d", rrhs[i]);
  292.     }
  293.  
  294.   fprintf(ftable, "\n};\n\nstatic short yyrhs[] = {%6d", ritem[0]);
  295.  
  296.   j = 10;
  297.   for (sp = ritem + 1; *sp; sp++)
  298.     {
  299.       putc(',', ftable);
  300.  
  301.       if (j >= 10)
  302.     {
  303.       putc('\n', ftable);
  304.       j = 1;
  305.     }
  306.       else
  307.     {
  308.       j++;
  309.     }
  310.  
  311.       if (*sp > 0)
  312.     fprintf(ftable, "%6d", *sp);
  313.       else
  314.     fprintf(ftable, "     0");
  315.     }
  316.  
  317.   fprintf(ftable, "\n};\n");
  318. }
  319.  
  320.  
  321.  
  322. output_stos()
  323. {
  324.   register int i;
  325.   register int j;
  326.  
  327.   fprintf(ftable, "\nstatic short yystos[] = {     0");
  328.  
  329.   j = 10;
  330.   for (i = 1; i < nstates; i++)
  331.     {
  332.       putc(',', ftable);
  333.  
  334.       if (j >= 10)
  335.     {
  336.       putc('\n', ftable);
  337.       j = 1;
  338.     }
  339.       else
  340.     {
  341.       j++;
  342.     }
  343.  
  344.       fprintf(ftable, "%6d", accessing_symbol[i]);
  345.     }
  346.  
  347.   fprintf(ftable, "\n};\n");
  348. }
  349.  
  350.  
  351.  
  352. output_rule_data()
  353. {
  354.   register int i;
  355.   register int j;
  356.  
  357.   fprintf(ftable, "\nstatic short yyr1[] = {     0");
  358.  
  359.   j = 10;
  360.   for (i = 1; i <= nrules; i++)
  361.     {
  362.       putc(',', ftable);
  363.  
  364.       if (j >= 10)
  365.     {
  366.       putc('\n', ftable);
  367.       j = 1;
  368.     }
  369.       else
  370.     {
  371.       j++;
  372.     }
  373.  
  374.       fprintf(ftable, "%6d", rlhs[i]);
  375.     }
  376.  
  377.   FREE(rlhs + 1);
  378.  
  379.   fprintf(ftable, "\n};\n\nstatic short yyr2[] = {     0");
  380.  
  381.   j = 10;
  382.   for (i = 1; i < nrules; i++)
  383.     {
  384.       putc(',', ftable);
  385.  
  386.       if (j >= 10)
  387.     {
  388.       putc('\n', ftable);
  389.       j = 1;
  390.     }
  391.       else
  392.     {
  393.       j++;
  394.     }
  395.  
  396.       fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
  397.     }
  398.  
  399.   putc(',', ftable);
  400.   if (j >= 10)
  401.     putc('\n', ftable);
  402.  
  403.   fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
  404.   FREE(rrhs + 1);
  405. }
  406.  
  407.  
  408.  
  409. output_defines()
  410. {
  411.   fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
  412.   fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
  413.   fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
  414. }
  415.  
  416.  
  417.  
  418. /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck.  */
  419.  
  420. output_actions()
  421. {
  422.   nvectors = nstates + nvars;
  423.  
  424.   froms = NEW2(nvectors, short *);
  425.   tos = NEW2(nvectors, short *);
  426.   tally = NEW2(nvectors, short);
  427.   width = NEW2(nvectors, short);
  428.  
  429.   token_actions();
  430.   free_shifts();
  431.   free_reductions();
  432.   FREE(lookaheads);
  433.   FREE(LA);
  434.   FREE(LAruleno);
  435.   FREE(accessing_symbol);
  436.  
  437.   goto_actions();
  438.   FREE(goto_map);
  439.   FREE(from_state);
  440.   FREE(to_state);
  441.  
  442.   sort_actions();
  443.   pack_table();
  444.   output_base();
  445.   output_table();
  446.   output_check();
  447. }
  448.  
  449.  
  450.  
  451. /* figure out the actions for the specified state, indexed by lookahead token type.
  452.  
  453.    The yydefact table is output now.  The detailed info
  454.    is saved for putting into yytable later.  */
  455.  
  456. token_actions()
  457. {
  458.   register int i;
  459.   register int j;
  460.   register int k;
  461.  
  462.   actrow = NEW2(ntokens, short);
  463.  
  464.   k = action_row(0);
  465.   fprintf(ftable, "\nstatic short yydefact[] = {%6d", k);
  466.   save_row(0);
  467.  
  468.   j = 10;
  469.   for (i = 1; i < nstates; i++)
  470.     {
  471.       putc(',', ftable);
  472.  
  473.       if (j >= 10)
  474.     {
  475.       putc('\n', ftable);
  476.       j = 1;
  477.     }
  478.       else
  479.     {
  480.       j++;
  481.     }
  482.  
  483.       k = action_row(i);
  484.       fprintf(ftable, "%6d", k);
  485.       save_row(i);
  486.     }
  487.  
  488.   fprintf(ftable, "\n};\n");
  489.   FREE(actrow);
  490. }
  491.  
  492.  
  493.  
  494. /* Decide what to do for each type of token if seen as the lookahead token in specified state.
  495.    The value returned is used as the default action (yydefact) for the state.
  496.    In addition, actrow is filled with what to do for each kind of token,
  497.    index by symbol number, with zero meaning do the default action.
  498.  
  499.    This is where conflicts are resolved.  The loop over lookahead rules
  500.    considered lower-numbered rules last, and the last rule considered that likes
  501.    a token gets to handle it.  */
  502.  
  503. int
  504. action_row(state)
  505. int state;
  506. {
  507.   register int i;
  508.   register int j;
  509.   register int k;
  510.   register int m;
  511.   register int n;
  512.   register int count;
  513.   int default_rule;
  514.   int nreds;
  515.   int maxcount;
  516.   int rule;
  517.   int shift_state;
  518.   int symbol;
  519.   unsigned mask;
  520.   register unsigned *wordp;
  521.   register reductions *redp;
  522.   register shifts *shiftp;
  523.   int nodefault = 0;  /* set nonzero to inhibit having any default reduction */
  524.  
  525.   for (i = 0; i < ntokens; i++)
  526.     actrow[i] = 0;
  527.  
  528.   default_rule = 0;
  529.   nreds = 0;
  530.   redp = reduction_table[state];
  531.  
  532.   if (redp)
  533.     {
  534.       nreds = redp->nreds;
  535.  
  536.       if (nreds >= 1)
  537.     {
  538.       /* loop over all the rules available here which require lookahead */
  539.       m = lookaheads[state];
  540.       n = lookaheads[state + 1];
  541.  
  542.       for (i = n - 1; i >= m; i--)
  543.         {
  544.           rule = - LAruleno[i];
  545.           wordp = LA + i * tokensetsize;
  546.           mask = 1;
  547.  
  548.           /* and find each token which the rule finds acceptable to come next */
  549.           for (j = 0; j < ntokens; j++)
  550.         {
  551.           /* and record this rule as the rule to use if that token follows.  */
  552.           if (mask & *wordp)
  553.             actrow[j] = rule;
  554.  
  555.           mask <<= 1;
  556.           if (mask == 0)
  557.             {
  558.               mask = 1;
  559.               wordp++;
  560.             }
  561.         }
  562.         }
  563.     }
  564.     }
  565.  
  566.   shiftp = shift_table[state];
  567.  
  568.   /* now see which tokens are allowed for shifts in this state.
  569.      For them, record the shift as the thing to do.  So shift is preferred to reduce.  */
  570.  
  571.   if (shiftp)
  572.     {
  573.       k = shiftp->nshifts;
  574.  
  575.       for (i = 0; i < k; i++)
  576.     {
  577.       shift_state = shiftp->shifts[i];
  578.       if (! shift_state) continue;
  579.  
  580.       symbol = accessing_symbol[shift_state];
  581.  
  582.       if (ISVAR(symbol))
  583.         break;
  584.  
  585.       actrow[symbol] = shift_state;
  586.  
  587.       /* do not use any default reduction if there is a shift for error */
  588.  
  589.       if (symbol == error_token_number) nodefault = 1;
  590.     }
  591.     }
  592.  
  593.   /* now find the most common reduction and make it the default action for this state.  */
  594.  
  595.   if (nreds >= 1 && ! nodefault)
  596.     {
  597.       if (consistent[state])
  598.     default_rule = redp->rules[0];
  599.       else
  600.     {
  601.       maxcount = 0;
  602.       for (i = m; i < n; i++)
  603.         {
  604.           count = 0;
  605.           rule = - LAruleno[i];
  606.     
  607.           for (j = 0; j < ntokens; j++)
  608.         {
  609.           if (actrow[j] == rule)
  610.             count++;
  611.         }
  612.     
  613.           if (count > maxcount)
  614.         {
  615.           maxcount = count;
  616.           default_rule = rule;
  617.         }
  618.         }
  619.     
  620.       /* actions which match the default are replaced with zero,
  621.          which means "use the default" */
  622.     
  623.       if (maxcount > 0)
  624.         {
  625.           for (j = 0; j < ntokens; j++)
  626.         {
  627.           if (actrow[j] == default_rule)
  628.             actrow[j] = 0;
  629.         }
  630.     
  631.           default_rule = - default_rule;
  632.         }
  633.     }
  634.     }
  635.  
  636.   return (default_rule);
  637. }
  638.  
  639.  
  640.  
  641. save_row(state)
  642. int state;
  643. {
  644.   register int i;
  645.   register int count;
  646.   register short *sp;
  647.   register short *sp1;
  648.   register short *sp2;
  649.  
  650.   count = 0;
  651.   for (i = 0; i < ntokens; i++)
  652.     {
  653.       if (actrow[i] != 0)
  654.     count++;
  655.     }
  656.  
  657.   if (count == 0)
  658.     return;
  659.  
  660.   froms[state] = sp1 = sp = NEW2(count, short);
  661.   tos[state] = sp2 = NEW2(count, short);
  662.  
  663.   for (i = 0; i < ntokens; i++)
  664.     {
  665.       if (actrow[i] != 0)
  666.     {
  667.       *sp1++ = i;
  668.       *sp2++ = actrow[i];
  669.     }
  670.     }
  671.  
  672.   tally[state] = count;
  673.   width[state] = sp1[-1] - sp[0] + 1;
  674. }
  675.  
  676.  
  677.  
  678. /* figure out what to do after reducing with each rule,
  679.    depending on the saved state from before the beginning
  680.    of parsing the data that matched this rule.
  681.  
  682.    The yydefgoto table is output now.  The detailed info
  683.    is saved for putting into yytable later.  */
  684.  
  685. goto_actions()
  686. {
  687.   register int i;
  688.   register int j;
  689.   register int k;
  690.  
  691.   state_count = NEW2(nstates, short);
  692.  
  693.   k = default_goto(ntokens);
  694.   fprintf(ftable, "\nstatic short yydefgoto[] = {%6d", k);
  695.   save_column(ntokens, k);
  696.  
  697.   j = 10;
  698.   for (i = ntokens + 1; i < nsyms; i++)
  699.     {
  700.       putc(',', ftable);
  701.  
  702.       if (j >= 10)
  703.     {
  704.       putc('\n', ftable);
  705.       j = 1;
  706.     }
  707.       else
  708.     {
  709.       j++;
  710.     }
  711.  
  712.       k = default_goto(i);
  713.       fprintf(ftable, "%6d", k);
  714.       save_column(i, k);
  715.     }
  716.  
  717.   fprintf(ftable, "\n};\n");
  718.   FREE(state_count);
  719. }
  720.  
  721.  
  722.  
  723. int
  724. default_goto(symbol)
  725. int symbol;
  726. {
  727.   register int i;
  728.   register int m;
  729.   register int n;
  730.   register int default_state;
  731.   register int maxcount;
  732.  
  733.   m = goto_map[symbol];
  734.   n = goto_map[symbol + 1];
  735.  
  736.   if (m == n)
  737.     return (-1);
  738.  
  739.   for (i = 0; i < nstates; i++)
  740.     state_count[i] = 0;
  741.  
  742.   for (i = m; i < n; i++)
  743.     state_count[to_state[i]]++;
  744.  
  745.   maxcount = 0;
  746.   default_state = -1;
  747.  
  748.   for (i = 0; i < nstates; i++)
  749.     {
  750.       if (state_count[i] > maxcount)
  751.     {
  752.       maxcount = state_count[i];
  753.       default_state = i;
  754.     }
  755.     }
  756.  
  757.   return (default_state);
  758. }
  759.  
  760.  
  761.  
  762. save_column(symbol, default_state)
  763. int symbol;
  764. int default_state;
  765. {
  766.   register int i;
  767.   register int m;
  768.   register int n;
  769.   register short *sp;
  770.   register short *sp1;
  771.   register short *sp2;
  772.   register int count;
  773.   register int symno;
  774.  
  775.   m = goto_map[symbol];
  776.   n = goto_map[symbol + 1];
  777.  
  778.   count = 0;
  779.   for (i = m; i < n; i++)
  780.     {
  781.       if (to_state[i] != default_state)
  782.     count++;
  783.     }
  784.  
  785.   if (count == 0)
  786.     return;
  787.  
  788.   symno = symbol - ntokens + nstates;
  789.  
  790.   froms[symno] = sp1 = sp = NEW2(count, short);
  791.   tos[symno] = sp2 = NEW2(count, short);
  792.  
  793.   for (i = m; i < n; i++)
  794.     {
  795.       if (to_state[i] != default_state)
  796.     {
  797.       *sp1++ = from_state[i];
  798.       *sp2++ = to_state[i];
  799.     }
  800.     }
  801.  
  802.   tally[symno] = count;
  803.   width[symno] = sp1[-1] - sp[0] + 1;
  804. }
  805.  
  806.  
  807.  
  808. /* the next few functions decide how to pack 
  809.    the actions and gotos information into yytable. */
  810.  
  811. sort_actions()
  812. {
  813.   register int i;
  814.   register int j;
  815.   register int k;
  816.   register int t;
  817.   register int w;
  818.  
  819.   order = NEW2(nvectors, short);
  820.   nentries = 0;
  821.  
  822.   for (i = 0; i < nvectors; i++)
  823.     {
  824.       if (tally[i] > 0)
  825.     {
  826.       t = tally[i];
  827.       w = width[i];
  828.       j = nentries - 1;
  829.  
  830.       while (j >= 0 && (width[order[j]] < w))
  831.         j--;
  832.  
  833.       while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  834.         j--;
  835.  
  836.       for (k = nentries - 1; k > j; k--)
  837.         order[k + 1] = order[k];
  838.  
  839.       order[j + 1] = i;
  840.       nentries++;
  841.     }
  842.     }
  843. }
  844.  
  845.  
  846.  
  847. pack_table()
  848. {
  849.   register int i;
  850.   register int place;
  851.   register int state;
  852.  
  853.   base = NEW2(nvectors, short);
  854.   pos = NEW2(nentries, short);
  855.   table = NEW2(MAXTABLE, short);
  856.   check = NEW2(MAXTABLE, short);
  857.  
  858.   lowzero = 0;
  859.   high = 0;
  860.  
  861.   for (i = 0; i < nvectors; i++)
  862.     base[i] = MINSHORT;
  863.  
  864.   for (i = 0; i < MAXTABLE; i++)
  865.     check[i] = -1;
  866.  
  867.   for (i = 0; i < nentries; i++)
  868.     {
  869.       state = matching_state(i);
  870.  
  871.       if (state < 0)
  872.     place = pack_vector(i);
  873.       else
  874.     place = base[state];
  875.  
  876.       pos[i] = place;
  877.       base[order[i]] = place;
  878.     }
  879.  
  880.   for (i = 0; i < nvectors; i++)
  881.     {
  882.       FREE(froms[i]);
  883.       FREE(tos[i]);
  884.     }
  885.  
  886.   FREE(froms);
  887.   FREE(tos);
  888.   FREE(pos);
  889. }
  890.  
  891.  
  892.  
  893. int
  894. matching_state(vector)
  895. int vector;
  896. {
  897.   register int i;
  898.   register int j;
  899.   register int k;
  900.   register int t;
  901.   register int w;
  902.   register int match;
  903.   int prev;
  904.  
  905.   i = order[vector];
  906.   if (i >= nstates)
  907.     return (-1);
  908.  
  909.   t = tally[i];
  910.   w = width[i];
  911.  
  912.   for (prev = vector - 1; prev >= 0; prev--)
  913.     {
  914.       j = order[prev];
  915.       if (width[j] != w || tally[j] != t)
  916.     return (-1);
  917.  
  918.       match = 1;
  919.       for (k = 0; match && k < t; k++)
  920.     {
  921.       if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  922.         match = 0;
  923.     }
  924.  
  925.       if (match)
  926.     return (j);
  927.     }
  928.  
  929.   return (-1);
  930. }
  931.  
  932.  
  933.  
  934. int
  935. pack_vector(vector)
  936. int vector;
  937. {
  938.   register int i;
  939.   register int j;
  940.   register int k;
  941.   register int t;
  942.   register int loc;
  943.   register int ok;
  944.   register short *from;
  945.   register short *to;
  946.  
  947.   i = order[vector];
  948.   t = tally[i];
  949.  
  950.   if (t == 0)
  951.     berror("pack_vector");
  952.  
  953.   from = froms[i];
  954.   to = tos[i];
  955.  
  956.   for (j = lowzero - from[0]; j < MAXTABLE; j++)
  957.     {
  958.       ok = 1;
  959.  
  960.       for (k = 0; ok && k < t; k++)
  961.     {
  962.       loc = j + from[k];
  963.       if (loc > MAXTABLE)
  964.         fatal("maximum table size exceeded");
  965.  
  966.       if (table[loc] != 0)
  967.         ok = 0;
  968.     }
  969.  
  970.       for (k = 0; ok && k < vector; k++)
  971.     {
  972.       if (pos[k] == j)
  973.         ok = 0;
  974.     }
  975.  
  976.       if (ok)
  977.     {
  978.       for (k = 0; k < t; k++)
  979.         {
  980.           loc = j + from[k];
  981.           table[loc] = to[k];
  982.           check[loc] = from[k];
  983.         }
  984.  
  985.       while (table[lowzero] != 0)
  986.         lowzero++;
  987.  
  988.       if (loc > high)
  989.         high = loc;
  990.  
  991.       return (j);
  992.     }
  993.     }
  994.  
  995.   berror("pack_vector");
  996. }
  997.  
  998.  
  999.  
  1000. /* the following functions output yytable, yycheck
  1001.    and the vectors whose elements index the portion starts */
  1002.  
  1003. output_base()
  1004. {
  1005.   register int i;
  1006.   register int j;
  1007.  
  1008.   fprintf(ftable, "\nstatic short yypact[] = {%6d", base[0]);
  1009.  
  1010.   j = 10;
  1011.   for (i = 1; i < nstates; i++)
  1012.     {
  1013.       putc(',', ftable);
  1014.  
  1015.       if (j >= 10)
  1016.     {
  1017.       putc('\n', ftable);
  1018.       j = 1;
  1019.     }
  1020.       else
  1021.     {
  1022.       j++;
  1023.     }
  1024.  
  1025.       fprintf(ftable, "%6d", base[i]);
  1026.     }
  1027.  
  1028.   fprintf(ftable, "\n};\n\nstatic short yypgoto[] = {%6d", base[nstates]);
  1029.  
  1030.   j = 10;
  1031.   for (i = nstates + 1; i < nvectors; i++)
  1032.     {
  1033.       putc(',', ftable);
  1034.  
  1035.       if (j >= 10)
  1036.     {
  1037.       putc('\n', ftable);
  1038.       j = 1;
  1039.     }
  1040.       else
  1041.     {
  1042.       j++;
  1043.     }
  1044.  
  1045.       fprintf(ftable, "%6d", base[i]);
  1046.     }
  1047.  
  1048.   fprintf(ftable, "\n};\n");
  1049.   FREE(base);
  1050. }
  1051.  
  1052.  
  1053.  
  1054. output_table()
  1055. {
  1056.   register int i;
  1057.   register int j;
  1058.  
  1059.   fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
  1060.   fprintf(ftable, "\nstatic short yytable[] = {%6d", table[0]);
  1061.  
  1062.   j = 10;
  1063.   for (i = 1; i <= high; i++)
  1064.     {
  1065.       putc(',', ftable);
  1066.  
  1067.       if (j >= 10)
  1068.     {
  1069.       putc('\n', ftable);
  1070.       j = 1;
  1071.     }
  1072.       else
  1073.     {
  1074.       j++;
  1075.     }
  1076.  
  1077.       fprintf(ftable, "%6d", table[i]);
  1078.     }
  1079.  
  1080.   fprintf(ftable, "\n};\n");
  1081.   FREE(table);
  1082. }
  1083.  
  1084.  
  1085.  
  1086. output_check()
  1087. {
  1088.   register int i;
  1089.   register int j;
  1090.  
  1091.   fprintf(ftable, "\nstatic short yycheck[] = {%6d", check[0]);
  1092.  
  1093.   j = 10;
  1094.   for (i = 1; i <= high; i++)
  1095.     {
  1096.       putc(',', ftable);
  1097.  
  1098.       if (j >= 10)
  1099.     {
  1100.       putc('\n', ftable);
  1101.       j = 1;
  1102.     }
  1103.       else
  1104.     {
  1105.       j++;
  1106.     }
  1107.  
  1108.       fprintf(ftable, "%6d", check[i]);
  1109.     }
  1110.  
  1111.   fprintf(ftable, "\n};\n");
  1112.   FREE(check);
  1113. }
  1114.  
  1115.  
  1116.  
  1117. /* copy the parser code into the ftable file at the end.  */
  1118.  
  1119. output_parser()
  1120. {
  1121.   register int c;
  1122.   FILE *fpars;
  1123.  
  1124.   if (pure_parser)
  1125.     fprintf(ftable, "#define YYIMPURE 1\n\n");
  1126.   else
  1127.     fprintf(ftable, "#define YYPURE 1\n\n");
  1128.  
  1129.   if (semantic_parser)
  1130.     fpars = fparser;
  1131.   else fpars = fparser1;
  1132.  
  1133.   c = getc(fpars);
  1134.   while (c != EOF)
  1135.     {
  1136.       if (c == '$')
  1137.         fprintf(ftable, "#include \"%s\"\n", actfile);
  1138.       else
  1139.     putc(c, ftable);
  1140.       c = getc(fpars);
  1141.     }
  1142. }
  1143.  
  1144.  
  1145.  
  1146. output_program()
  1147. {
  1148.   register int c;
  1149.   extern int lineno;
  1150.  
  1151.   fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
  1152.  
  1153.   c = getc(finput);
  1154.   while (c != EOF)
  1155.     {
  1156.       putc(c, ftable);
  1157.       c = getc(finput);
  1158.     }
  1159. }
  1160.  
  1161.  
  1162.  
  1163. free_itemsets()
  1164. {
  1165.   register core *cp, *cptrail;
  1166.  
  1167.   FREE(state_table);
  1168.  
  1169.   for (cp = first_state; cp;) {
  1170.     cptrail = cp;
  1171.     cp = cp->next;
  1172.     FREE(cptrail);
  1173.   }
  1174. }
  1175.  
  1176.  
  1177.  
  1178. free_shifts()
  1179. {
  1180.   register shifts *sp, *sptrail;
  1181.  
  1182.   FREE(shift_table);
  1183.  
  1184.   for (sp = first_shift; sp;) {
  1185.     sptrail = sp;
  1186.     sp = sp->next;
  1187.     FREE(sptrail);
  1188.   }
  1189. }
  1190.  
  1191.  
  1192.  
  1193. free_reductions()
  1194. {
  1195.   register reductions *rp, *rptrail;
  1196.  
  1197.   FREE(reduction_table);
  1198.  
  1199.   for (rp = first_reduction; rp;) {
  1200.     rptrail = rp;
  1201.     rp = rp->next;
  1202.     FREE(rptrail);
  1203.   }
  1204. }
  1205.  
  1206. emit (stp)
  1207. char **stp;
  1208. {
  1209.     while (stp && *stp) {
  1210.     fprintf (faction, "%s", *stp++);
  1211.     }
  1212. }
  1213.